<template>
  <div class="coupon-packet-container">
    <!-- 操作 -->
    <el-row class="coupon-packet-row">
      <el-col :span="14" style="text-align:left;padding-left:15px" class="coupon-packet-row-col">
        <el-button type="primary" plain size="small" style="padding:8px 30px;" @click="add">新 增</el-button>
      </el-col>
      <el-col :span="10" style="text-align:right;padding-right:40px">
        <el-input v-model="searchName" placeholder="请输入名称" size="small" style="max-width:240px" @keydown.enter="search">
          <el-button slot="suffix" type="text" size="small" class="search-button" @click="search">
            <i class="el-icon-search"></i>
          </el-button>
        </el-input>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <div>
      <el-table :data="couponList" style="width:100%" stripe>
        <el-table-column prop="name" label="类型名称" align="center"></el-table-column>
        <el-table-column prop="price" label="面值" align="center"></el-table-column>
        <el-table-column prop="full_price" label="满减金额" align="center"></el-table-column>
        <el-table-column prop="count" label="发行总量" align="center"></el-table-column>
        <el-table-column prop="from_date" label="开始时间" align="center"></el-table-column>
        <el-table-column prop="to_date" label="结束时间" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope" width="80px">
            <el-switch v-model="scope.row.status" active-color="#13ce66" active-value="1" inactive-value="0" @change="changeStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button class="action-button" type="text" 
              @click="eidt(scope.row)"><i class="el-icon-edit"/></el-button>
            <el-button class="action-button" 
              @click="del(scope.row)" type="text">
              <svg-icon icon-class="shanchu"/>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增 编辑 -->
    <el-dialog :visible.sync="disAdd" width="30%" :title="isAdd ? '新增' : '修改'" top="0">
      <add-shop :isAdd="isAdd" :packetData="packetData" :type="type" @success="disAdd = false; updata()" v-if="disAdd"/>
    </el-dialog>
  </div>
</template>

<script>
// import mixin from './mixin'
import AddShop from './AddShop'
import { delCoupon, putCoupon } from '@/api/marketing'

export default {
  name: 'ShopPacket',
  // mixins: [mixin],
  components: {
    AddShop
  },
  props: {
    couponList: {
      type: Array,
      required: true
    },
    type: {
      type: [String,Number],
      required: true
    }
  },
  data() {
    return {
      // type: '1',
      searchName: '',
      disAdd: false,
      isAdd: true,
      packetData: {
        key: this.$store.state.app.activeApp.saa_key,
        name: '',
        type: '',
        category_id: '',
        goods_id: '',
        price: '',
        min_price: '',
        lucky_price: '',
        lucky_min_price: '',
        full_price: '',
        receive_count: '',
        count: '',
        from_date: '',
        to_date: '',
        status: '1',
      }
    }
  },
  methods: {
    /**
     * 新增
     */
    add() {
      this.packetData = {
        key: this.$store.state.app.activeApp.saa_key,
        name: '',
        type: this.type,
        category_id: '',
        goods_id: '',
        price: '',
        min_price: '',
        lucky_price: '',
        lucky_min_price: '',
        full_price: '',
        receive_count: '',
        count: '',
        from_date: '',
        to_date: '',
        status: '1',
      }
      this.disAdd = true;
      this.isAdd = true;
    },
    /**
     * 编辑
     */
    eidt(data) {
      this.packetData = {
        key: this.$store.state.app.activeApp.saa_key,
        name: data.name,
        type: this.type,
        category_id: data.category_id,
        goods_id: data.goods_id,
        price: parseInt(data.price),
        min_price: parseInt(data.min_price),
        lucky_price: parseInt(data.lucky_price),
        lucky_min_price: parseInt(data.lucky_min_price),
        full_price: parseInt(data.full_price),
        receive_count: parseInt(data.receive_count),
        count: parseInt(data.count),
        from_date: data.from_date,
        to_date: data.to_date,
        status: data.status,
        id: data.id
      };
      this.disAdd = true;
      this.isAdd = false;
    },
    /**
     * 删除
     */
    del(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: data.id
      }
      delCoupon(params).then(response => {
        if(response.status === 200){
          this.$emit('updata')
          this.$message.success('删除成功！');
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 修改状态
     */
    changeStatus(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: data.id,
        status: data.status
      };

      putCoupon(params).then(res => {
        if(res.status === 200) {
          this.$message.success('修改成功！');
        }else{
          this.$message.error(res.message);
          this.$emit('updata');
        }
      })
    },
    /**
     * 请求父组件刷新数据
     */
    updata() {
      this.$emit('updata')
    },
    /**
     * 搜索
     */
    search() {
      this.$emit('search',this.searchName)
    }
  },
}
</script>

<style lang="scss" scoped>

</style>